package com.shop.controller;

import com.shop.common.SetMeta;
import com.shop.entity.Attribute;
import com.shop.service.AttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class AttributeController {

    @Autowired
    private AttributeService attributeService;

    /**
     * 参数列表
     * @param id
     * @param sel
     * @return
     */
    @PreAuthorize("hasAuthority('142')")
    @GetMapping("categories/{id}/attributes")
    public SetMeta<List<Attribute>> selectList(@PathVariable int id, @RequestParam String sel) {
        return attributeService.selectList(id, sel);
    }

    /**
     * 添加动态参数或静态参数
     * @param id
     * @param attributes
     * @return
     */
    @PreAuthorize("hasAuthority('143')")
    @PostMapping("categories/{id}/attributes")
    public SetMeta<Attribute> addAttribute(@PathVariable int id, @RequestBody Attribute attributes) {
        return attributeService.addAttribute(id, attributes);
    }

    /**
     * 删除参数
     * @param id
     * @param attrId
     * @return
     */
    @PreAuthorize("hasAuthority('144')")
    @DeleteMapping("categories/{id}/attributes/{attrId}")
    public SetMeta<Attribute> deleteAttribute(@PathVariable int id, @PathVariable int attrId) {
        return attributeService.deleteAttribute(id, attrId);
    }

    /**
     * 根据ID查询参数
     * @param id
     * @param attrId
     * @return
     */
    @PreAuthorize("hasAuthority('158')")
    @GetMapping("categories/{id}/attributes/{attrId}")
    public SetMeta<Attribute> selectOne(@PathVariable int id, @PathVariable int attrId) {
        return attributeService.selectOne(id, attrId);
    }

    /**
     * 编辑提交参数
     * @param id
     * @param attrId
     * @param attribute
     * @return
     */
    @PreAuthorize("hasAuthority('161')")
    @PutMapping("categories/{id}/attributes/{attrId}")
    public SetMeta<Attribute> updateAttribute(@PathVariable int id,@PathVariable int attrId,@RequestBody Attribute attribute) {
        return attributeService.updateAttribute(id, attrId, attribute);
    }
}